<?php

/**
 * 用户需求
 */

namespace app\api\controller;


use app\common\controller\ApiController;
use app\common\lib\RespCode;
use think\Db;
use think\Exception;
use app\api\model\AgentDemand as AgentDemandModel;
use app\api\model\Agent as AgentModel;

class AgentDemand extends ApiController {

    protected $isArrayResult = ['showdemand','newshowdemand','gethelpdemand'];

    protected $resubmitLock = ['editpushstatus'];

    /**
     * ver 1.0.1 1.0.2
     * 添加需求和编辑需求
     * @return false|string
     */
    public function editDemand() {
        try {
            $params = $this->checkParams('area_code,city_code,p_key,a_key,type,h_key');
            if ($params === false) {
                return $this->resErr();
            }
            $re = AgentDemandModel::edit($params);
            if ($re) {
                return $this->resData('操作成功', []);
            }else{
                return $this->resErr();
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 添加/编辑需求（不限区域）
     * @return false|string
     */
    public function editDemandUnlimited() {
        try {
            $params = $this->checkParams('area_code,city_code,p_key,a_key,type,h_key');
            if ($params === false) {
                return $this->resErr();
            }
            $re = AgentDemandModel::editUnlimited($params);
            if ($re) {
                return $this->resData('操作成功', []);
            }else{
                return $this->resErr();
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }
    /**
     * ver 1.0.0
     * 展示需求
     */
    public function showDemand() {
        try {
            $params = $this->checkParams('');
            if ($params === false) {
                return $this->resErr();
            }
            $re = AgentDemandModel::show($params);
            return $this->resData('success', $re);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 展示需求
     */
    public function newShowDemand() {
        try {
            $params = $this->checkParams('');
            if ($params === false) {
                return $this->resErr();
            }
            $re = AgentDemandModel::newShow($params);
            return $this->resData('success', $re);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 删除需求
     */
    public function delDemand() {
        try {
            $params = $this->checkParams('id');
            if ($params === false) {
                return $this->resErr();
            }
            if (empty(db('agent_demand')->getByid($params['id'])))
                throw new Exception('数据不存在', RespCode::DATA_ERR);
            $re = AgentDemandModel::del($params);
            if (!$re) {
                throw new Exception('操作失败', RespCode::QUERY_ERR);
            } else {
                return $this->resData('操作成功', []);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 修改推送状态
     * @return false|string
     */
    public function editPushStatus() {
        try {
            $params = $this->checkParams('');
            if ($params === false) {
                return $this->resErr();
            }
            AgentModel::editPushStatus($params);
            return $this->resData('操作成功', []);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }


    /**
     * ver 安卓1.0.2  ios1.0.1
     * 帮填需求
     * @return false|string
     */
    public function addHelpUserDemand(){
        try {
            $params = $this->checkParams('area_code,city_code,p_key,a_key,type,h_key,user_id');
            if ($params === false) {
                return $this->resErr();
            }
            $AgentDemandModel=new AgentDemandModel();
            $data=$AgentDemandModel->addHelpUserDemand($params);
            if ($data){
                return $this->resData('操作成功', []);
            }else{
                $this->resErr();
            }

        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 帮填需求
     * @return false|string
     */
    public function addHelpUserDemandUnlimited(){
        try {
            $params = $this->checkParams('area_code,city_code,p_key,a_key,type,h_key,user_id');
            if ($params === false) {
                return $this->resErr();
            }
            $AgentDemandModel=new AgentDemandModel();
            $data=$AgentDemandModel->addHelpUserDemandUnlimited($params);
            if ($data){
                return $this->resData('操作成功', []);
            }else{
                $this->resErr();
            }

        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }
    /**
     * 获取帮填需求展示
     * @return false|string
     */
    public function getHelpDemand(){
        try {
            $params = $this->checkParams('user_id');
            if ($params === false) {
                return $this->resErr();
            }
            $AgentDemandModel=new AgentDemandModel();
            $data=$AgentDemandModel->getHelpDemand($params);
            return $this->resData('success', $data);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取定制选房的结果房源
     * @return false|string
     */
    public function getDemandHouse(){
        try {
            $params = $this->checkParams('id,page');
            if ($params === false) {
                return $this->resErr();
            }
            $pageSize=10;//默认每页显示数据条数
            $AgentDemandModel=new AgentDemandModel();
            $data=$AgentDemandModel->getDemandHouse($params,$pageSize);
            return $this->resData('success', $data);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 定制选房 条件筛选数据
     * @return false|string
     */
    public function getDemandWhere(){
        try {
            $params = $this->checkParams('id');
            if ($params === false) {
                return $this->resErr();
            }
            $AgentDemandModel=new AgentDemandModel();
            $data=$AgentDemandModel->getDemandWhere($params);
            return $this->resData('success', $data);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }





}
